import pandas, duckdb
from time import strftime
import os
from utils.path_utils import get_project_root
from database.duckdb.duckdb_base import get_connection



# timenow = str(strftime("%Y%m%d%H%M%S"))
def run(dwbh, dwid, kjnd, timenow):
  conn = get_connection()
  conn.execute("""
               delete from T_API_XM where fiyear = ? and ACCOUNTORG = ?;
               """, [kjnd, dwid])
  conn.execute("""
    insert into T_API_XM (ID, accountorg, cusitemproperty, cusitemcode, cusitemname_chs, cusitemfullname_chs, fiyear, isdisabled, orgcode, orgname, parentid, timestamp_createdon,
               timestamp_lastchangedon, esun_api_orgid, dwbh, esun_api_update, esun_api_dwbh, parent_treeinfo_isdetail)
              select
                Gsdm||'-'||Xmdm,
                '"""+dwid+"""',
                '项目类型_名称',
                Xmdm,
                Xmmc,Xmmc,
                '"""+kjnd+"""',
                Syzt,
                '"""+dwbh+"""',
                Bm,
                '父节点信息_父节点字段',
                STRPTIME(Jl_RQ, '%Y%m%d'),
                STRPTIME(Xg_RQ, '%Y%m%d'),
                '"""+dwid+"""',
                '"""+dwbh+"""',
                '"""+timenow+"""',
                '"""+dwbh+"""',
                1,
               from GL_Xmzl;
  """)

  conn.close()



# run('03047', '01051507', '2024', r'D:\code\app_report_back20241201\会计科目_数据导出.xlsx', timenow)